package view;

import java.awt.Dimension;
import java.beans.PropertyVetoException;

import javax.swing.BorderFactory;

import control.Controlador;
import control.ControladorDisciplina;

import model.Disciplina;
import patterns.Bean;
import util.Constantes;
import util.Detalhar;
import util.components.buttons.JButtonBuscar;
import util.components.buttons.JButtonCancelar;
import util.components.buttons.JButtonOK;

public class JanelaDisciplina extends javax.swing.JInternalFrame implements Bean{

	public static final int ADICIONAR = 0;
	public static final int REMOVER = 1;
	public static final int EDITAR = 2;
	public static final int CONSULTAR = 3;
	
	private Disciplina bean;
	private Controlador control;
	private boolean remover = false, editar = false, consultar = false;
	private int tipoJanela;
	
	public JanelaDisciplina(String title){
		this(title, false, false, false);
	}

	public JanelaDisciplina(String title, boolean remover, boolean editar, boolean consultar){
		super(title);
		this.remover = remover;
		this.editar = editar;
		this.consultar = consultar;
		control = new ControladorDisciplina(this);
		initComponents();
		if(remover){
			setTipoJanela(JanelaDisciplina.REMOVER);
			detalhar();
		}else if(editar){
			setTipoJanela(JanelaDisciplina.EDITAR);
			detalhar();
		}else if(consultar){
			setTipoJanela(JanelaDisciplina.CONSULTAR);
			detalhar();
		}else{
			setTipoJanela(JanelaDisciplina.ADICIONAR);
		}
		control.registraEventos();
	}	

	public void initComponents(){

		java.awt.GridBagConstraints gridBagConstraints;

		panelDadosDisciplina = new javax.swing.JPanel();
		panelBusca = new javax.swing.JPanel();
		panelBotao = new javax.swing.JPanel();
		btnOk = new JButtonOK(control, "Ok");
		btnCancelar = new JButtonCancelar(this, "Cancelar");
		btnBuscar = new JButtonBuscar(control, "Buscar");
		lblCodDisc = new javax.swing.JLabel();
		lblNomeDisc = new javax.swing.JLabel();
		lblCreditos = new javax.swing.JLabel();
		txtCodDisc = new javax.swing.JTextField();
		txtNomeDisc = new javax.swing.JTextField();
		txtCreditos = new javax.swing.JTextField();

		setLayout(new java.awt.GridBagLayout());

		panelBusca = new javax.swing.JPanel();
		panelBusca.setLayout(new java.awt.GridBagLayout());

		panelDadosDisciplina.setBorder(BorderFactory.createTitledBorder("Disciplina"));
		panelDadosDisciplina.setLayout(new java.awt.GridBagLayout());

		lblCodDisc.setText("Codigo da Disciplina:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosDisciplina.add(lblCodDisc, gridBagConstraints);

		txtCodDisc.setPreferredSize(new Dimension(100, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		panelDadosDisciplina.add(txtCodDisc, gridBagConstraints);

		lblNomeDisc.setText("Nome:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosDisciplina.add(lblNomeDisc, gridBagConstraints);

		txtNomeDisc.setPreferredSize(new Dimension(220, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		panelDadosDisciplina.add(txtNomeDisc, gridBagConstraints);

		lblCreditos.setText("Creditos:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosDisciplina.add(lblCreditos, gridBagConstraints);

		txtCreditos.setPreferredSize(new Dimension(30, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		panelDadosDisciplina.add(txtCreditos, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		gridBagConstraints.weightx = 100.0;
		gridBagConstraints.weighty = 100.0;
		getContentPane().add(panelDadosDisciplina, gridBagConstraints);

		btnOk.setText("Ok");
		btnCancelar.setText("Cancelar");

		panelBotao.add(btnOk);
		panelBotao.add(btnCancelar);

		if(remover || editar || consultar){
			btnBuscar.setPreferredSize(new Dimension(80, 20));
			panelBusca.add(btnBuscar);

			gridBagConstraints = new java.awt.GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 1;
			gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
			gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new java.awt.Insets(2, 1, 1, 1);
			gridBagConstraints.weightx = 100.0;
			getContentPane().add(panelBusca, gridBagConstraints);
		}

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		gridBagConstraints.weightx = 100.0;
		getContentPane().add(panelBotao, gridBagConstraints);

		setVisible(true);
		setClosable(true);
		setResizable(true);
		setSize(new Dimension(Constantes.TAMANHO_JANELA_FILHA));
		setLocation(Constantes.POSICAO_X, Constantes.POSICAO_Y);
	}

	private javax.swing.JPanel panelDadosDisciplina;
	private javax.swing.JPanel panelBusca;
	private javax.swing.JPanel panelBotao;
	private JButtonOK btnOk;
	private JButtonCancelar btnCancelar;
	private JButtonBuscar btnBuscar;
	private javax.swing.JLabel lblCodDisc;
	private javax.swing.JLabel lblNomeDisc;
	private javax.swing.JLabel lblCreditos;
	private javax.swing.JTextField txtCodDisc;
	private javax.swing.JTextField txtNomeDisc;
	private javax.swing.JTextField txtCreditos;

	public void detalhar(){
		Detalhar.campoTexto(txtNomeDisc);
		Detalhar.campoTexto(txtCreditos);
		Detalhar.detalharBotao(btnOk);
	}
	
	public void desdetalhar(){
		Detalhar.desdetalharCampoTexto(txtNomeDisc);
		Detalhar.desdetalharCampoTexto(txtCreditos);
		if(!consultar)
			Detalhar.desdetalharBotao(btnOk);
	}
	
	public void consultar(){
		panelBotao.remove(btnCancelar);
	}

	public Object getBean() {

		if(this.bean == null)
			bean = new Disciplina();

		if(txtCodDisc.getText() != null)
			this.bean.setCodigoDisc(Long.parseLong(txtCodDisc.getText()));

		if(txtNomeDisc.getText() != null)
			this.bean.setNomeDisc(txtNomeDisc.getText());

		if(txtCreditos.getText() != null)
			this.bean.setCreditos(Integer.parseInt(txtCreditos.getText()));

		return bean;
	}

	public void setBean(Object bean) {
		this.bean = (Disciplina)bean;

		if(this.bean != null){
			txtCodDisc.setText(String.valueOf(this.bean.getCodigoDisc()));
			txtNomeDisc.setText(this.bean.getNomeDisc());
			txtCreditos.setText(String.valueOf(this.bean.getCreditos()));
		}
	}
	
	public int getTipoJanela() {
		return tipoJanela;
	}

	public void setTipoJanela(int tipoJanela) {
		this.tipoJanela = tipoJanela;
	}

	public JButtonOK getBtnOk() {
		return btnOk;
	}

	public JButtonCancelar getBtnCancelar() {
		return btnCancelar;
	}

	public JButtonBuscar getBtnBuscar() {
		return btnBuscar;
	}

	public javax.swing.JTextField getTxtCodDisc() {
		return txtCodDisc;
	}

	public javax.swing.JTextField getTxtNomeDisc() {
		return txtNomeDisc;
	}

	public javax.swing.JTextField getTxtCreditos() {
		return txtCreditos;
	}
}
